package com.nip.workmanager.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.nip.workmanagement.R;
import com.nip.workmanager.common.ConstantDatabase;
import com.nip.workmanager.common.ConstantIntentCode;
import com.nip.workmanager.database.TodoTable;
import com.nip.workmanager.entity.TodoEntity;

/**
 * @author PhamKieu
 * 
 */
public class AddToDoActivity extends Activity {

	private Button btnAddTodo;
	private Button btnBack;
	private EditText edTodoName;
	private EditText edTodoDes;
	private ToggleButton tgButton;
	private Boolean isOn = false;
	private TextView tvTime;
	private String _WorkId;
	private Intent i;
	private TodoTable _todoTable;
	private TodoEntity _mTodo;
	private Boolean _isRegist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_to_do_activity);
		Initialize();
	}

	/**
	 * 
	 */
	private void Initialize() {
		btnAddTodo = (Button) findViewById(R.id.btnRegistTodo);
		btnBack = (Button) findViewById(R.id.btnBackTodoList);
		edTodoName = (EditText) findViewById(R.id.edTodoName);
		edTodoDes = (EditText) findViewById(R.id.edTodoDescription);
		tgButton = (ToggleButton) findViewById(R.id.tgbtnONOff);
		tvTime = (TextView) findViewById(R.id.tvTime);
		edTodoName.setFocusable(true);

		btnAddTodo.setOnClickListener(new MyEvent());
		btnBack.setOnClickListener(new MyEvent());
		tvTime.setOnClickListener(new MyEvent());
		tgButton.setOnClickListener(new MyEvent());
		SimpleDateFormat formart = new SimpleDateFormat(
				ConstantDatabase.DATE_TIME_FORMAT);
		Date now = new Date();
		String currentTime = formart.format(now);
		tvTime.setText(currentTime);

		i = getIntent();
		Bundle bundle = i.getExtras();
		_mTodo = (TodoEntity) bundle
				.getSerializable(ConstantIntentCode.TODO_ENTITY);
		_isRegist = bundle.getBoolean(ConstantIntentCode.REGIST_UPDATE_TODO);
		if (_isRegist) {
			_WorkId = _mTodo.getWorkId();
		} else {
			edTodoName.setText(_mTodo.getToDoName());
			edTodoDes.setText(_mTodo.getDescription());
			tvTime.setText(_mTodo.getTime());
			tgButton.setChecked(_mTodo.getToDoStatus());
			isOn = _mTodo.getToDoStatus();
		}

		_todoTable = new TodoTable(AddToDoActivity.this);
	}

	/**
	 * 
	 */
	private void RegistTodo() {
		if (_mTodo == null) {
			_mTodo = new TodoEntity();
			_mTodo.setWorkId(_WorkId);
		}
		_mTodo.setDescription(edTodoDes.getText().toString());
		_mTodo.setToDoName(edTodoName.getText().toString());
		_mTodo.setTime(tvTime.getText().toString());
		_mTodo.setToDoStatus(isOn);
		if (_todoTable.InsertTodo(_mTodo)) {
			Toast.makeText(AddToDoActivity.this, "Insert Success !",
					Toast.LENGTH_LONG).show();
			i.putExtra(ConstantIntentCode.REGIST_TODO_RESULT, true);
			setResult(ConstantIntentCode.RESULT_ADD_TODO_CODE, i);
			finish();
		} else {
			Toast.makeText(AddToDoActivity.this, "Insert Fail !",
					Toast.LENGTH_LONG).show();
		}
	}

	/**
	 * 
	 */
	private void UpdateTodo() {
		if (_mTodo == null) {
			return;
		}

		_mTodo.setDescription(edTodoDes.getText().toString());
		_mTodo.setToDoName(edTodoName.getText().toString());
		_mTodo.setTime(tvTime.getText().toString());
		_mTodo.setToDoStatus(isOn);

		if (_todoTable.UpdateTodo(_mTodo)) {
			Toast.makeText(AddToDoActivity.this, "UpdateTodo Success !",
					Toast.LENGTH_LONG).show();
			i.putExtra(ConstantIntentCode.REGIST_TODO_RESULT, true);
			setResult(ConstantIntentCode.RESULT_ADD_TODO_CODE, i);
			finish();
		} else {
			Toast.makeText(AddToDoActivity.this, "UpdateTodo Fail !",
					Toast.LENGTH_LONG).show();
		}
	}

	private void UpdateStatus(ToggleButton toggleButton) {
		isOn = toggleButton.isChecked();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}

	private class MyEvent implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnRegistTodo:
				if (_isRegist)
					RegistTodo();
				else
					UpdateTodo();
				break;
			case R.id.btnBackTodoList:
				onBackPressed();
				break;
			case R.id.tvTime:

				break;
			case R.id.tgbtnONOff:
				UpdateStatus((ToggleButton) v);
				break;
			default:
				break;
			}
		}

	}
}
